﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Itop.YHPC.Model;
using Itop.YHPC.controller;

namespace Itop.YHPC.admin {
    public partial class yhpcOrgAjax :AjaxPage<yhpcOrg> {
        protected override IList<yhpcOrg> onRead() {
            if (!string.IsNullOrEmpty(Request.QueryString["type"]))
                return yhpcOrgController.GetList("where dwtype='" + Request.QueryString["type"] + "'  order by orderid");
            else
                return yhpcOrgController.GetList(" order by orderid");
        }
        protected override bool onCreate(IList<yhpcOrg> list) {
            foreach (yhpcOrg obj in list) {
                obj.id = obj.CreateID();
                obj.dwOwner = getParentDw(obj);
                yhpcOrgController.Insert(obj);
            }
            return true;
        }
        protected override bool onUpdate(IList<yhpcOrg> list) {
            foreach (yhpcOrg obj in list) {

                yhpcOrgController.Update(obj);
            }
            return true;
        }

        protected override bool onDelete(IList<yhpcOrg> list) {
            foreach (yhpcOrg obj in list) {
                yhpcOrgController.Delete(obj.id);
            }
            return true;
        }

        private string getParentDw(yhpcOrg org) {
            string dw="";

            yhpcOrg org2 = org;
            while (org2 != null) {
                if (org2.ParentID == "root") {
                    dw = org2.dwName; break;
                }
                org2 = yhpcOrgController.Bll.Get(org2.ParentID);
            }
            return dw;
        }
    }
}
